package com.wanli.agent.homepage;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.TextView;

import com.wanli.agent.R;
import com.wanli.agent.base.BaseActivity;
import com.wanli.agent.base.DataCallBack;
import com.wanli.agent.bean.DeviceUnifyReceivedNewBean;
import com.wanli.agent.homepage.model.HomePageModelImpl;
import com.wanli.agent.widget.AppTitleBar;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class MachinesDetailsActivity extends BaseActivity {
    @BindView(R.id.title_bar)
    AppTitleBar titleBar;
    @BindView(R.id.tv_total_num)
    TextView tvTotalNum;
    @BindView(R.id.tv_able_use_count)
    TextView tvAbleUseCount;
    @BindView(R.id.tv_look1)
    TextView tvLook1;
    @BindView(R.id.tv_used_count)
    TextView tvUsedCount;
    @BindView(R.id.tv_look2)
    TextView tvLook2;
    @BindView(R.id.tv_leave_count)
    TextView tvLeaveCount;
    @BindView(R.id.tv_look3)
    TextView tvLook3;
    @BindView(R.id.tv_disable)
    TextView tvDisable;
    @BindView(R.id.tv_look4)
    TextView tvLook4;
    private String type;
    private String child_agent_id;
    private String typeUnit;
    private String agent_name;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_machines_details);
        ButterKnife.bind(this);
        initView();
    }

    @Override
    protected void onResume() {
        super.onResume();
        initData();
    }

    private void initView() {
        type = getIntent().getStringExtra("type");
        child_agent_id = getIntent().getStringExtra("child_agent_id");
        agent_name = getIntent().getStringExtra("agent_name");
        String titlePrefix = "";
        if (!TextUtils.isEmpty(agent_name)) {
            titlePrefix = agent_name + "_";
            if (agent_name.length() > 6) {
                titlePrefix = agent_name.substring(0, 6) + "_";
            }
        } else {
            titlePrefix = "";
        }
        if ("1".equals(type)) {
            titleBar.setTitle(titlePrefix + "扫码盒");
            typeUnit = "(台)";
        } else if ("3".equals(type)) {
            titleBar.setTitle(titlePrefix + "码牌");
            typeUnit = "(张)";
        } else if ("4".equals(type)) {
            titleBar.setTitle(titlePrefix + "云音响");
            typeUnit = "(台)";
        } else if ("6".equals(type)) {
            titleBar.setTitle(titlePrefix + "码牌音响");
            typeUnit = "(台)";
        } else if ("8".equals(type)) {
            titleBar.setTitle(titlePrefix + "收款码");
            typeUnit = "(张)";
        }
    }

    private void initData() {
        new HomePageModelImpl().requestChildDeviceStatisticsByUse(child_agent_id, type, new DataCallBack<DeviceUnifyReceivedNewBean>() {
            @Override
            public void onSuccessful(DeviceUnifyReceivedNewBean bean) {
                tvAbleUseCount.setText(bean.getData().getChild_un_use() + typeUnit);
                tvUsedCount.setText(bean.getData().getChild_use() + typeUnit);
                tvLeaveCount.setText(bean.getData().getNot_child() + typeUnit);
                tvDisable.setText(bean.getData().getChild_disable() + typeUnit);
                int totalNum = bean.getData().getChild_un_use() + bean.getData().getChild_use() + bean.getData().getNot_child() + bean.getData().getChild_disable();
                tvTotalNum.setText(totalNum + "");
            }

            @Override
            public void onFailed(String msg, String code) {

            }
        });
    }

    @OnClick({R.id.tv_look1, R.id.tv_look2, R.id.tv_look3, R.id.tv_look4})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.tv_look1:
                //未使用
                Intent intent = new Intent(mActivity, MachinesListActivity.class);
                intent.putExtra("type", type);
                intent.putExtra("status", "0");
                intent.putExtra("child_agent_id", child_agent_id);
                intent.putExtra("agent_name", agent_name);
                startActivity(intent);
                break;
            case R.id.tv_look2:
                //已使用
                intent = new Intent(mActivity, MachinesListActivity.class);
                intent.putExtra("type", type);
                intent.putExtra("status", "1");
                intent.putExtra("child_agent_id", child_agent_id);
                intent.putExtra("agent_name", agent_name);
                startActivity(intent);
                break;
            case R.id.tv_look4:
                //已禁用
                intent = new Intent(mActivity, MachinesListActivity.class);
                intent.putExtra("type", type);
                intent.putExtra("status", "2");
                intent.putExtra("child_agent_id", child_agent_id);
                intent.putExtra("agent_name", agent_name);
                startActivity(intent);
                break;
            case R.id.tv_look3:
                //已划拨
                intent = new Intent(mActivity, TransferAgentListActivity.class);
                intent.putExtra("type", type);
                intent.putExtra("child_agent_id", child_agent_id);
                intent.putExtra("agent_name", agent_name);
                startActivity(intent);
                break;
        }
    }
}
